package com.lepu.app.fun.tool.bbxxjs;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.ui.wheel.WheelView;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.widget.MyDialog;
import com.eyzhs.app.R;
import com.lepu.app.main.activity.AppBroswerActivity;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class BbxxjsActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener {

	private int mCurrentMyBlood = 0;
	private int mCurrentOtherBlood = 0;

	String[] mCanTypeArray = null;
	String[] mCanNotTypeArray = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		mNeedStatusBar = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.index_bbxxjs_activity);

		UtilityBase.setStatusBarRes(this, R.color.color_bmi, true);
		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}
	
	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}


	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("宝宝血型计算器");
		topbar.setTopbarBackground(R.color.color_bmi);
		topbar.setonTopbarNewLeftLayoutListener(this);

		TextView bottomTipTextView = (TextView) findViewById(R.id.bottomTipTextView);
		bottomTipTextView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
		bottomTipTextView.getPaint().setAntiAlias(true);

		mCanTypeArray = getResources().getStringArray(R.array.array_tool_bbxxjs_blood_type_can);
		mCanNotTypeArray = getResources().getStringArray(R.array.array_tool_bbxxjs_blood_type_can_not);
	}

	private void selectBloodType(View view, final int type)
	{
		final TextView textView = (TextView) view;

		final String[] array = getResources().getStringArray(R.array.array_tool_bbxxjs_blood_type);
		int index = 0;
		if(type == 0)
		{
			index = mCurrentMyBlood;
		}else{
			index = mCurrentOtherBlood;
		}

		final MyDialog dialog = new MyDialog(this);
		dialog.setSimpleSelectPicker(array, index)
				.setTitle("请选择血型")
				.setNegativeButton(R.string.app_cancel,
						new View.OnClickListener() {

							@Override
							public void onClick(View v) {

							}
						})
				.setPositiveButton(R.string.app_ok,
						new View.OnClickListener() {

							@Override
							public void onClick(View v) {

								WheelView wv_content = (WheelView) v
										.findViewById(R.id.simpleSelectPicker);
								int index = wv_content.getCurrentItem();

								textView.setText(array[index]);

								if (type == 0) {
									mCurrentMyBlood = index;
								} else {
									mCurrentOtherBlood = index;
								}
							}
						}).create(null).show();
	}

	private void submit()
	{
		TextView myBloodTypeValueTextView = (TextView) findViewById(R.id.myBloodTypeValueTextView);
		TextView otherBloodTypeValueTextView = (TextView) findViewById(R.id.otherBloodTypeValueTextView);

		String myBloodType = myBloodTypeValueTextView.getText().toString().trim();
		String otherBloodType = otherBloodTypeValueTextView.getText().toString().trim();

		if(TextUtils.isEmpty(myBloodType) && TextUtils.isEmpty(otherBloodType))
		{
			UIHelper.showToast(this,"请您先填写血型,再查看结果");
			return;
		}

		if(TextUtils.isEmpty(myBloodType))
		{
			UIHelper.showToast(this,"您的血型未填写");
			return;
		}

		if(TextUtils.isEmpty(otherBloodType))
		{
			UIHelper.showToast(this,"您配偶的血型未填写");
			return;
		}

		int index = 0;
		if((mCurrentMyBlood == 0 && mCurrentOtherBlood == 0))
		{
			index = 0;
		}else if((mCurrentMyBlood == 0 && mCurrentOtherBlood == 3) ||
			(mCurrentMyBlood == 3 && mCurrentOtherBlood == 0))
		{
			index = 1;
		}else if((mCurrentMyBlood == 0 && mCurrentOtherBlood == 1) ||
				(mCurrentMyBlood == 1 && mCurrentOtherBlood == 0))
		{
			index = 2;
		}else if((mCurrentMyBlood == 0 && mCurrentOtherBlood == 2) ||
				(mCurrentMyBlood == 2 && mCurrentOtherBlood == 0))
		{
			index = 3;
		}else if((mCurrentMyBlood == 1 && mCurrentOtherBlood == 1))
		{
			index = 4;
		}else if((mCurrentMyBlood == 1 && mCurrentOtherBlood == 3) ||
				(mCurrentMyBlood == 3 && mCurrentOtherBlood == 1))
		{
			index = 5;
		}else if((mCurrentMyBlood == 1 && mCurrentOtherBlood == 2) ||
				(mCurrentMyBlood == 2 && mCurrentOtherBlood == 1))
		{
			index = 6;
		}else if((mCurrentMyBlood == 2 && mCurrentOtherBlood == 3) ||
				(mCurrentMyBlood == 3 && mCurrentOtherBlood == 2))
		{
			index = 7;
		}else if((mCurrentMyBlood == 2 && mCurrentOtherBlood == 2))
		{
			index = 8;
		}else if((mCurrentMyBlood == 3 && mCurrentOtherBlood == 3))
		{
			index = 9;
		}

		String resultCan = mCanTypeArray[index];
		String resultNoCan = mCanNotTypeArray[index];

		Intent intent = new Intent(this,BbxxjsResultActivity.class);
		intent.putExtra("resultCan",resultCan);
		intent.putExtra("resultNoCan", resultNoCan);
		startActivity(intent,true);
	}

	public void viewClick(View view)
	{
		int rid = view.getId();

		switch (rid)
		{
			case R.id.myBloodTypeValueTextView:

				selectBloodType(view,0);
				break;
			case R.id.otherBloodTypeValueTextView:

				selectBloodType(view,1);
				break;
			case R.id.bottomTipTextView:

				Intent intent = new Intent(this, AppBroswerActivity.class);
				intent.putExtra("url", Setting.getBabyXingGeUrl());//test
				intent.putExtra("top_color_id",R.color.color_bmi);
				startActivity(intent, true);
				break;
			case R.id.testButton:

				submit();
				break;
			default:
				break;
		}
	}
}
